package com.qf.proxy.service;

import cn.hutool.core.util.RandomUtil;
import com.qf.proxy.entity.Info;
import com.qf.proxy.entity.MeshInfo;
//import com.qf.proxy.util.HttpClientUtil;
import com.qf.proxy.util.ProxyInfo;
import com.qf.proxy.util.http.HttpClientConfigZhang;
import com.qf.proxy.util.http.HttpClientUtilZhang;
import com.qf.proxy.util.zhanghttp.HttpClientConfig;
import com.qf.proxy.util.zhanghttp.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 葛成瑞
 * @crete 2021/1/5
 */
@Service
@Slf4j
public class HandleData {

    public static List<String> ips = new ArrayList<>();




    //处理下载数据
    public static String getPubmedData(String format, String ids) {
        String res = "";
        String url = "https://pubmed.ncbi.nlm.nih.gov/results-export-ids/";
        BasicCookieStore bcs = new BasicCookieStore();
        String refer = "https://pubmed.ncbi.nlm.nih.gov/";
        ProxyInfo pi = new ProxyInfo();
        Map<String, String[]> map = new LinkedHashMap<>();
//        map.put("csrfmiddlewaretoken", new String[]{"A719o5hY4As9ReRc2QeuJS5kBOSvRyhupoLpBPF07o8pC7KRAgfUKVfr51SqSAdK"});
//        map.put("results-format", new String[]{format});
//        map.put("result-ids", new String[]{ids});

        BasicClientCookie cookie = new BasicClientCookie("pm-csrf", "hKNo3SZ2A1MXmbZJ5k6m939k5W0tFf4J61xEgCn4DPsd74SoDK7Ma6jrz90oGh0Z");
        cookie.setVersion(0);
        cookie.setDomain("pubmed.ncbi.nlm.nih.gov");
        cookie.setPath("/");
        bcs.addCookie(cookie);
        List<NameValuePair> param = new ArrayList<>();
        param.add(new BasicNameValuePair("csrfmiddlewaretoken", "A719o5hY4As9ReRc2QeuJS5kBOSvRyhupoLpBPF07o8pC7KRAgfUKVfr51SqSAdK"));
        param.add(new BasicNameValuePair("results-format", format));
        param.add(new BasicNameValuePair("result-ids", ids));
        String s2 = RandomUtil.randomEle(ips, ips.size());
        HttpClientConfig loginConfig = new HttpClientConfig().setUrl(url).setReferer(refer).setNvpsList(param).setCookieStore(bcs).setLocalIp(s2).build();
        log.info("当前ip++" + s2);
        String s1 = bcs.toString();
        System.out.println(s1);
        loginConfig.setCookieStore(bcs);
        res = HttpClientUtil.post(loginConfig);
        return res;
    }

    public static void main(String[] args) {

        String pmid = getPubmedData("pubmed", "1,2,3,4,5,6");
        System.out.println(pmid);


//        String pubmedDataParam = getPubmedDataParam("pmid", "ju", "simsearch2.ffrft!@#datesearch.y_5");
//        System.out.println(pubmedDataParam);


    }

    public static String getMeshDetailHtml(String param) {
        String ipInfo = RandomUtil.randomEle(ips, ips.size());
        log.info("当前ip++" + ipInfo);

        String URL = "https://www.ncbi.nlm.nih.gov/mesh/" + param;
        HttpClientConfig loginConfig = new HttpClientConfig().setUrl(URL).setLocalIp(ipInfo).build();

        String s = HttpClientUtil.get(loginConfig);
        if (StringUtils.isEmpty(s)) {
            for (int i = 0; i < 3; i++) {
                if (StringUtils.isNotEmpty(s)) {
                    break;
                }
            }
        }
        return s;

    }


    public static String getHtml(String param) {
        String URL = "https://www.ncbi.nlm.nih.gov/mesh/";
//        Map<String, Object> paramMap = new LinkedHashMap<>();
//        paramMap.put("p$l", "AjaxServer");
//        paramMap.put("portlets", "id=meshpmbuilder:db=mesh:uid=" + param + ";id=meshdiscodblinks:db=mesh:uid=" + param + ";id=recentactivity");
//        paramMap.put("load", "yes");
        String ipInfo = RandomUtil.randomEle(ips, ips.size());
        log.info("当前ip++" + ipInfo);
        List<NameValuePair> param2 = new ArrayList<>();
        param2.add(new BasicNameValuePair("p$l", "AjaxServer"));
        param2.add(new BasicNameValuePair("portlets", "id=meshpmbuilder:db=mesh:uid=" + param + ";id=meshdiscodblinks:db=mesh:uid=" + param + ";id=recentactivity"));
        param2.add(new BasicNameValuePair("load", "yes"));
        HttpClientConfig loginConfig = new HttpClientConfig().setUrl(URL).setNvpsList(param2).setLocalIp(ipInfo).build();

        String s = HttpClientUtil.post(loginConfig);
        if (StringUtils.isEmpty(s)) {
            for (int i = 0; i < 3; i++) {
                s = getHtml(param);
                if (StringUtils.isNotEmpty(s)) {
                    break;
                }
            }
        }
        return s;

    }


    public static Info getInfoByHtml(String html) {
        Document parse = Jsoup.parse(html);
        Info info = new Info();
        info.setSearchkey(parse.select("#term").attr("value"));
        info.setSessionid(parse.select("#SessionId").attr("value"));
        info.setTotal(Integer.valueOf(parse.select("#resultcount").attr("value")));
        if (!StringUtils.isEmpty(parse.select("#pageno2").attr("value"))) {
            info.setMaxpage(Integer.valueOf(parse.select("#pageno2").attr("value")));
        } else {
            info.setMaxpage(0);
        }

        Elements els = parse.select(".rprt");
        for (Element el : els) {
            MeshInfo meshInfo = new MeshInfo();
            meshInfo.setNum(el.select("span").text());
            meshInfo.setUrl(el.select(".title a").attr("href"));
            meshInfo.setTitle(el.select(".title a").text());
            meshInfo.setDesc(el.select(".desc").html());
            meshInfo.setDetails(el.select(".details").text());
            meshInfo.setAux(el.select(".aux").text());
            meshInfo.setLinks(el.select(".links").html());
            info.getMeshInfoList().add(meshInfo);
        }
        return info;
    }

    public static Info GetFirstMesh(String key, Integer page, Integer limit, String sessionid, Integer total) {

        String ipInfo = RandomUtil.randomEle(ips, ips.size());
        log.info("当前ip++" + ipInfo);
        if (page == 1) {
            log.info("页码是1++++检索词+++" + key);
            //2021年5月12日10:29:55 不知道为什么 发送前转码了 发送过来是decode的形式 还是得encode一下
            HttpClientConfigZhang config = new HttpClientConfigZhang();
            config.setUrl("https://www.ncbi.nlm.nih.gov/mesh?term=" + URLEncoder.encode(key));
            config.setLocalIp(ipInfo);
            config.build();
            String s = HttpClientUtilZhang.get(config);
            if (StringUtils.isEmpty(s)) {
                for (int i = 0; i < 3; i++) {
                    s = HttpClientUtilZhang.get(config);
                    if (StringUtils.isNotEmpty(s)) {
                        break;
                    }
                }
            }
            if (s == null) {
                return null;
            }
            Info infoByHtml = getInfoByHtml(s);
            return infoByHtml;
        } else {
            String html = getMeshByPage(key, sessionid, page, total);
            if (StringUtils.isEmpty(html)) {
                for (int i = 0; i < 3; i++) {
                    html = getMeshByPage(key, sessionid, page, total);
                    if (StringUtils.isNotEmpty(html)) {
                        break;
                    }

                }
            }
            if (html == null) {
                return null;
            }
            Info info = getInfoByHtml(html);
            return info;
        }
    }

    public static String getMeshByPage(String term, String sessionid, Integer page, Integer total) {
        HttpClientConfigZhang config = new HttpClientConfigZhang();
        config.setUrl("https://www.ncbi.nlm.nih.gov/mesh");
        config.setCookie("ncbi_sid=" + sessionid);
        config.getNvpsList().add(new BasicNameValuePair("term", term));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_PageController.PreviousPageName", "results"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.sPresentation", "DocSum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.sPageSize", "20"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.FFormat", "Docsum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.FileFormat", "docsum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.LastPresentation", "docsum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.Presentation", "docsum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.PageSize", "20"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.LastPageSize", "20"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.Format", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.LastFormat", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.PrevPageSize", "20"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.PrevPresentation", "docsum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.PrevSort", ""));
        config.getNvpsList().add(new BasicNameValuePair("CollectionStartIndex", "1"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_ResultsController.ResultCount", total + ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_ResultsController.RunLastQuery", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Entrez_Pager.cPage", (page - 1) + ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Entrez_Pager.CurrPage", page + ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Entrez_Pager.cPage", (page - 1) + ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.sPresentation2", "DocSum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.sPageSize2", "20"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Mesh_DisplayBar.FFormat2", "Docsum"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.Db", "mesh"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.LastDb", "mesh"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.Term", term));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.LastTabCmd", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.LastQueryKey", "1"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.IdsFromResult", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.LastIdsFromResult", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.LinkName", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.LinkReadableName", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.LinkSrcDb", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.Cmd", "PageChanged"));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.TabCmd", ""));
        config.getNvpsList().add(new BasicNameValuePair("EntrezSystem2.PEntrez.DbConnector.QueryKey", ""));
        config.getNvpsList().add(new BasicNameValuePair("p$a", "EntrezSystem2.PEntrez.Mesh.Mesh_ResultsPanel.Entrez_Pager.Page"));
        config.getNvpsList().add(new BasicNameValuePair("p$l", "EntrezSystem2"));
        config.getNvpsList().add(new BasicNameValuePair("p$st", "mesh"));
        String ipInfo = RandomUtil.randomEle(ips, ips.size());
        log.info("当前ip++" + ipInfo);
        config.setLocalIp(ipInfo).build();
        String post = HttpClientUtilZhang.post(config);
        return post;
    }


    //根据检索式导出pmid 最大是10000 根据过滤检索
    //用 !@ 进行切割
    public static String getPubmedDataParam(String format, String param, String sort,String filter) {
        log.info("转换后的检索参数" + param);
        String[] split = new String[]{};
        if (StringUtils.isNotEmpty(filter)) {
            split = filter.split("!@");
        }
        String ipInfo = RandomUtil.randomEle(ips, ips.size());
        log.info("当前ip++" + ipInfo);

        String res = "";
        String url = "https://pubmed.ncbi.nlm.nih.gov/results-export-search-data/";
        BasicCookieStore bcs = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("pm-csrf", "qFYCReQ3wzHSVKHFNDE9X6PSIfeCwpmq7NGvfPJLeKz2lBJGTwUnq8sTwauYHSeG");
        cookie.setVersion(0);
        cookie.setDomain("pubmed.ncbi.nlm.nih.gov");
        cookie.setPath("/");
        bcs.addCookie(cookie);
        String refer = "https://pubmed.ncbi.nlm.nih.gov/";
//        ProxyInfo pi = new ProxyInfo();
//        Map<String, String[]> map = new LinkedHashMap<>();
//        map.put("csrfmiddlewaretoken", new String[]{"7PAhN5poiKtandY5yQJKaE6sBdOifK9XOXiabGi60VlkN406EJZYDGJtp84Eqd1d"});
//        map.put("results-format", new String[]{format});
//        map.put("term", new String[]{param});
//        if (split.length != 0) {
//            map.put("filter", split);
//        }

        List<NameValuePair> param2 = new ArrayList<>();
        param2.add(new BasicNameValuePair("csrfmiddlewaretoken", "7PAhN5poiKtandY5yQJKaE6sBdOifK9XOXiabGi60VlkN406EJZYDGJtp84Eqd1d"));
        param2.add(new BasicNameValuePair("results-format", format));
        param2.add(new BasicNameValuePair("term", param));
        if (split.length != 0) {
            for (String s : split) {
                param2.add(new BasicNameValuePair("filter", s));
            }

        }
        if (StringUtils.isNotEmpty(sort)) {
            param2.add(new BasicNameValuePair("sort", sort));
        }
        System.out.println("param2+"+param2);
//        com.qf.proxy.util.HttpClientConfig loginConfig = new com.qf.proxy.util.HttpClientConfig(pi, url, refer, true);
        HttpClientConfig loginConfig = new HttpClientConfig().setUrl(url).setReferer(refer).setNvpsList(param2).setCookieStore(bcs).setLocalIp(ipInfo).build();
        res = HttpClientUtil.post(loginConfig);

//        res = com.qf.proxy.util.HttpClientUtil.postByMapArray(map, loginConfig);
        return res;
    }

    //根据检索式导出pmid 最大是10000
    public static String getPubmedDataParam(String format, String param) {
        log.info("转换后的检索参数" + param);
        String res = "";
        String ipInfo = RandomUtil.randomEle(ips, ips.size());
        log.info("当前ip++" + ipInfo);
        String url = "https://pubmed.ncbi.nlm.nih.gov/results-export-search-data/";
        BasicCookieStore bcs = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie("pm-csrf", "qFYCReQ3wzHSVKHFNDE9X6PSIfeCwpmq7NGvfPJLeKz2lBJGTwUnq8sTwauYHSeG");
        cookie.setVersion(0);
        cookie.setDomain("pubmed.ncbi.nlm.nih.gov");
        cookie.setPath("/");
        bcs.addCookie(cookie);
        String refer = "https://pubmed.ncbi.nlm.nih.gov/";
//        ProxyInfo pi = new ProxyInfo();
//        Map<String, String[]> map = new LinkedHashMap<>();

//        map.put("csrfmiddlewaretoken", new String[]{"7PAhN5poiKtandY5yQJKaE6sBdOifK9XOXiabGi60VlkN406EJZYDGJtp84Eqd1d"});
//        map.put("results-format", new String[]{format});
//        map.put("term", new String[]{param});

        List<NameValuePair> param2 = new ArrayList<>();
        param2.add(new BasicNameValuePair("csrfmiddlewaretoken", "7PAhN5poiKtandY5yQJKaE6sBdOifK9XOXiabGi60VlkN406EJZYDGJtp84Eqd1d"));
        param2.add(new BasicNameValuePair("results-format", format));
        param2.add(new BasicNameValuePair("term", param));

//        com.qf.proxy.util.HttpClientConfig loginConfig = new com.qf.proxy.util.HttpClientConfig(pi, url, refer, true);
        HttpClientConfig loginConfig = new HttpClientConfig().setUrl(url).setReferer(refer).setNvpsList(param2).setCookieStore(bcs).setLocalIp(ipInfo).build();

        String s1 = bcs.toString();
        System.out.println(s1);
        loginConfig.setCookieStore(bcs);
        //        res = com.qf.proxy.util.HttpClientUtil.postByMapArray(map, loginConfig);
        res = HttpClientUtil.post(loginConfig);
        return res;
    }


}
